package main

import (
	"net"
	"fmt"
	"os"
	"github.com/google/gopacket/layers"
	"github.com/google/gopacket"
)

func main() {
	addr := net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 9999}
	paddr := net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 10000}
	con, err := net.ListenUDP("udp", &addr)
	pcon, perr := net.DialUDP("udp", nil, &paddr)

	if err != nil || perr != nil {
		fmt.Print("Connection error.", err)
	}

	defer pcon.Close()
	defer con.Close()
	for {
		handleClient(con, pcon)
	}
}
func handleClient(conn, pcon *net.UDPConn) {
	data := make([]byte, 1024)

	n, _, err := conn.ReadFromUDP(data)
	if err != nil {
		fmt.Print("Connection error:", err)
		os.Exit(1)
	}

	fmt.Printf("%s\n", data[:n])

	ipv4 := layers.IPv4{
		SrcIP: net.IP{127, 0, 0, 1},
		DstIP: net.IP{8, 8, 8, 8},
		IHL:5,
		Version:4,
		TOS:0,
		Flags:5,
		TTL:64,
		Protocol:17,

	}

	udp:=layers.UDP{
		DstPort:2222,
		SrcPort:1111,
		Checksum:1222,
	}

	// And create the packet with the layers
	options := gopacket.SerializeOptions{}

	buffer := gopacket.NewSerializeBuffer()
	gopacket.SerializeLayers(buffer, options,
 		&ipv4,
		&udp,
		gopacket.Payload(data[:n]),
	)

	n, err = pcon.Write(buffer.Bytes())

	if err != nil {
		fmt.Print("Connection error:", err)
		os.Exit(1)
	}
}
